var form;
var tableIns;
var ctxPath = '/'
$(function () {
    layui.config({
        base: ctxPath + 'lib/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'//主入口模块
    }).use(['index', 'table', 'form','echarts'], function () {
        echarts = layui.echarts;
        // 基于准备好的dom，初始化echarts实例
        var day = echarts.init(document.getElementById('stat'));
        $.ajax({
            type: "get",
            url:"/audit/loginLog/loginLogStat",
            data: {},
            success:function(data){
                console.log(data)
                if(data.code == 0){
                    var arr1 = [];
                    var op = {
                        title: {
                            text: '登录日志类型统计'
                        },
                        tooltip: {},
                        legend: {
                            data:['总数量']
                        },
                        xAxis: {
                            data: []
                        },
                        yAxis: {},
                        series: [{
                            name: '总数量',
                            type: 'bar',
                            data: [],
                        }
                        ]
                    };
                    // 日统计数据封装显示
                    var statData = data.data;
                    if (statData == null)
                        return;
                    var xAxisData = []
                    for (var i=0; i < statData.length; i++){
                        //operate_type
                        console.log('111:'+statData[i].OPERATE_TYPE)
                        if(statData[i].OPERATE_TYPE.replace(/(^\s*)|(\s*$)/g, "") == '0'){
                            xAxisData.push("登录操作")
                            arr1.push(statData[i].TOTAL);
                        }else if (item.OPERATE_TYPE == '-1'){
                            xAxisData.push("登出操作")
                            arr1.push(statData[i].TOTAL);
                        }
                    }
                    op.xAxis.data = xAxisData;
                    op.series[0].data = arr1;
                    day.setOption(op);
                    console.log(day)
                }
            }});


        var table = layui.table;
        form = layui.form;
        // 渲染表格
        tableIns = table.render({
            elem: '#table',
            toolbar: '#table-title',
            url: 'loginLogPager',
            page: true,
            cols: [[
                {type: 'numbers'},
                {
                    field: 'organization', title: '查验区名称', align: 'center', templet: function (d) {
                        var organization = "";
                        $.ajax({
                            type: "GET",
                            data: {"cyqxh": d.organization},
                            async: false,
                            url: ctxPath + "system/organization/name",
                            success: function (res) {
                                organization = res.obj;
                            }
                        });
                        return organization;
                    }
                },
                {field: 'user_name', title: '用户名', align: 'center'},
                {field: 'terminal_id', title: '登录IP', align: 'center'},
                {
                    field: 'operate_time', title: '操作时间', align: 'center', templet: function (d) {
                        var value = d.operate_time;
                        return value.substring(0, 4) + '-'
                            + value.substring(4, 6)
                            + '-'
                            + value.substring(6, 8)
                            + '  '
                            + value.substring(8, 10)
                            + ':'
                            + value.substring(10, 12)
                            + ':'
                            + value.substring(12, 14);
                    }
                },
                {
                    field: 'operate_type', title: '操作类型', align: 'center', templet: function (d) {
                        if (d.operate_type == 0) {
                            return "登陆操作";
                        } else if (d.operate_type == -1) {
                            return "登出操作"
                        } else {
                            return "未知操作";
                        }
                    }
                },
                {field: 'operate_condition', title: '操作内容', align: 'center'},
                {field: 'right', title: '操作', toolbar: '#table-toolbar', align: 'center'},
            ]],
            done: function (res, curr, count) {
                // merge(res);
                // $('tr').css({'background-color': '#009688', 'color': '#fff'});//表头样式修改

                var that = this.elem.next();
                res.data.forEach(function (item, index) {
                    // var jyw = hex_md5(item.loginname + item.name + item.sfzhm + item.status);
                    if (item.isupdate === 1) {
                        var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                        tr.css("background-color", "red");
                        tr.find(".laytable-cell-1-0-9").css("color","red");
                    }
                });
            },
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'limit' //每页数据量的参数名，默认：limit
            }
        });
        initOrganSelect();
        //监听行工具事件
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'view') {
                openView(data)
            }
        });
        //监听提交
        form.on('submit(searchSubmit)', function () {
            tableIns.reload({
                where: {
                    organization: $("#cyqxhSearch").val(),
                    user_name: $("#userNameSearch").val(),
                    terminal_id: $("#terminalIdSearch").val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
    });
})

/**
 * 打开编辑窗口
 * @param data layui中保存的table中的对应的此条信息
 */
function openView(data) {
    layer.open({
        type: 1,
        title: "登录日志信息",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['700px', '500px'],
        content: $('#loginLogEdit'),
        success: function () {
            form.val('loginLogForm', data);
            renderForm()
        }, end: function () {
            cleanForm("loginLogForm")
        }
    });
}

function initOrganSelect() {
    $.ajax({
        type: "GET",
        url: ctxPath + "system/organization/organList",
        success: function (res) {
            var data = res.obj;
            for (var i = 0; i < data.length; i++) {
                $("#cyqxhSearch").append("<option value='" + data[i].cyqxh + "'>" + data[i].name + "</option>")
            }
            renderForm();
        }
    });
}